package org.mspring.mlog.duoshuo;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论导入请求参数
 *
 * @author Gao Youbo
 * @since 2015-02-13 18:29
 */
public class ImportRequest {
    private String shortName;
    private String secret;
    private List<Post> posts = new ArrayList<>();

    /**
     * 根据对象构建请求参数
     *
     * @return
     */
    public List<NameValuePair> buildParams() {
        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("short_name", this.shortName));
        nvps.add(new BasicNameValuePair("secret", this.secret));
        for (int i = 0; i < posts.size(); i++) {
            nvps.addAll(posts.get(i).buildParams(i));
        }
        return nvps;
    }


    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }

    public static class Post {
        /**
         * 必须属性
         */
        private String postKey; //这条评论在当前站点的ID
        private String threadKey; //这条评论所对应文章在当前站点的ID
        private String message; //评论内容

        /**
         * 可选属性
         */
        private String postId; //对于从多说导出的数据，再次导入多说，会根据此id进行匹配，自动忽略post_key参数
        private String parentKey; //父级评论在当前站点的ID
        private String authorKey; //评论者在当前站点的ID
        private String authorName; //评论者的用户名
        private String authorEmail; //评论者的邮箱，如果没有设置头像会根据这个信息从gravatar获取头像
        private String authorUrl; //评论者的URL，评论者头像或者名字会跳转到改URL
        private String ip; //评论者的IP
        private String agent; //评论者User Agent信息，通常包括浏览器版本、引擎、设备等信息
        private int likes; //这条评论被【赞】的次数，该属性导入意义不大，会在被喜欢之后重新统计
        private int reports; //对这条评论点了【举报】的次数
        private String createdAt; //评论发表时间。请注意时间的格式为yyyy-mm-dd hh:MM:ss。如2012/12/12 12:12:12和 2012-1-12 12:12:12的格式，都不会被成功识别，第二个时间是因为月份应为01
        private String status; //评论状态。支持：approved：已通过，pending：待审核，deleted：已删除。不建议将已删除数据导入多说，会在到期30天之后自动从数据库中清除。


        public List<NameValuePair> buildParams(int index) {
            List<NameValuePair> nvps = new ArrayList<>();
            nvps.add(new BasicNameValuePair("posts[" + index + "][post_key]", postKey));
            nvps.add(new BasicNameValuePair("posts[" + index + "][thread_key]", threadKey));
            nvps.add(new BasicNameValuePair("posts[" + index + "][message]", message));

            nvps.add(new BasicNameValuePair("posts[" + index + "][post_id]", postId));
            nvps.add(new BasicNameValuePair("posts[" + index + "][parent_key]", parentKey));
            nvps.add(new BasicNameValuePair("posts[" + index + "][author_key]", authorKey));
            nvps.add(new BasicNameValuePair("posts[" + index + "][author_name]", authorName));
            nvps.add(new BasicNameValuePair("posts[" + index + "][author_email]", authorEmail));
            nvps.add(new BasicNameValuePair("posts[" + index + "][author_url]", authorUrl));
            nvps.add(new BasicNameValuePair("posts[" + index + "][ip]", ip));
            nvps.add(new BasicNameValuePair("posts[" + index + "][agent]", agent));
            nvps.add(new BasicNameValuePair("posts[" + index + "][likes]", String.valueOf(likes)));
            nvps.add(new BasicNameValuePair("posts[" + index + "][reports]", String.valueOf(reports)));
            nvps.add(new BasicNameValuePair("posts[" + index + "][created_at]", createdAt));
            nvps.add(new BasicNameValuePair("posts[" + index + "][status]", status));
            return nvps;
        }

        public String getPostKey() {
            return postKey;
        }

        public void setPostKey(String postKey) {
            this.postKey = postKey;
        }

        public String getThreadKey() {
            return threadKey;
        }

        public void setThreadKey(String threadKey) {
            this.threadKey = threadKey;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getPostId() {
            return postId;
        }

        public void setPostId(String postId) {
            this.postId = postId;
        }

        public String getParentKey() {
            return parentKey;
        }

        public void setParentKey(String parentKey) {
            this.parentKey = parentKey;
        }

        public String getAuthorKey() {
            return authorKey;
        }

        public void setAuthorKey(String authorKey) {
            this.authorKey = authorKey;
        }

        public String getAuthorName() {
            return authorName;
        }

        public void setAuthorName(String authorName) {
            this.authorName = authorName;
        }

        public String getAuthorEmail() {
            return authorEmail;
        }

        public void setAuthorEmail(String authorEmail) {
            this.authorEmail = authorEmail;
        }

        public String getAuthorUrl() {
            return authorUrl;
        }

        public void setAuthorUrl(String authorUrl) {
            this.authorUrl = authorUrl;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public String getAgent() {
            return agent;
        }

        public void setAgent(String agent) {
            this.agent = agent;
        }

        public int getLikes() {
            return likes;
        }

        public void setLikes(int likes) {
            this.likes = likes;
        }

        public int getReports() {
            return reports;
        }

        public void setReports(int reports) {
            this.reports = reports;
        }

        public String getCreatedAt() {
            return createdAt;
        }

        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }
}
